*###########################################################*
* Effects of Settlement into Ethnic Enclaves on Immigrant   *
* Voter Turnout                                             *
* Andersson, Lajevardi, Lindgren, and Oskarsson             *
* Journal: Journal of Politics                              *
* Stata Replication Code                                    *
*###########################################################*

***************************************************
* 3. MANAGEMENT IND. SAMPLE (REFUGEES 1988-1991)  *
***************************************************

///SET DIRECTORY/log-files/set more off

clear all
cd "D:\SCB_ConPol\Stata"
set more off

*capture log close
*log using "C:\Userdata\Shared\Logs\Ethnic Enclaves, political participation\log_indsample", replace


*********************************************
* CREATE SAMPLE OF IMMIGRANTS FOR 1988-1991 *
*********************************************

foreach y in 87 88 89 90 91 {
///OPEN AND PREPARE MIGRATION FILE - basic sample of those immigrating year `y'

use "RTB\migrationer"

*Create immigration variable - keep immigrants

tab PostTyp, gen(m)
	keep if m1==1
	drop PostTyp m1 m2
 
*Generate year, month of arrival - keep only year in question 

tostring Datum, replace
gen im_year = substr(Datum, 1, 4) 
destring im_year, replace

gen im_month = substr(Datum, 5, 2) 
destring im_month, replace

	keep if im_year==19`y'

*small share who immigrated twise during a year - drop

duplicates tag LopNr, gen(tag)
sort tag LopNr

*Keep the observation from the latest immigration month
bysort LopNr: egen latestmonth = max(im_month)
	drop if im_month!=latestmonth & tag>0
		*duplicates report LopNr - OK
	drop tag latestmonth

///MERGE WITH INDIVIDUAL DATA (RTB) - THEY HAVE TO HAVE A PLACE OF RESIDENCE IN SWEDEN

*Use 1:m beacause there are some id-number duplicates in RTB-file
merge 1:m LopNr using "RTB\RTB_19`y'"
	keep if _merge==3
	drop _merge

*Small number of duplicates remaining - (19 ind. in 1990, 8 ind 1987 for example) - throw out
duplicates tag LopNr, gen(tag)
	drop if tag==1
	drop tag SenPnr /*no variation*/

	
///MERGE WITH BIRTH COUNTRY DATA - ONLY WANT IMM FROM CERTAIN COUNTRIES
merge 1:m LopNr using "RTB\FodelselandIFAUGrupp"
	keep if _merge==3
	drop _merge SenPnr /*no variation*/

***Now create fill in the information on country of birth (labels)

lab def IFAUkod 0 "Sweden"
lab def IFAUkod 26 "Finland", add
lab def IFAUkod 27 "Denmark", add
lab def IFAUkod 28 "Norway & Iceland", add
lab def IFAUkod 29 "Bosnia", add
lab def IFAUkod 30 "Yugoslavia", add
lab def IFAUkod 31 "Poland", add
lab def IFAUkod 32 "UK & Ireland", add
lab def IFAUkod 33 "Germany", add
lab def IFAUkod 34 "Mediterranian", add
lab def IFAUkod 35 "Baltic", add
lab def IFAUkod 36 "East Europe", add
lab def IFAUkod 37 "Hungary & Czech & Slovakia", add
lab def IFAUkod 38 "Benelux & Swizz & Austria", add
lab def IFAUkod 39 "US & Canada", add
lab def IFAUkod 40 "Central America", add
lab def IFAUkod 41 "Chile", add
lab def IFAUkod 42 "South America", add
lab def IFAUkod 43 "East Africa", add
lab def IFAUkod 44 "Mena", add
lab def IFAUkod 45 "Other Africa", add
lab def IFAUkod 46 "Iran", add
lab def IFAUkod 47 "Iraq", add  
lab def IFAUkod 48 "Turkey", add
lab def IFAUkod 49 "East Asia", add
lab def IFAUkod 50 "South East Asia", add
lab def IFAUkod 51 "South Asia", add
lab def IFAUkod 52 "Oceania", add
lab def IFAUkod 53 "Unknown or Stateless", add

lab values IFAUkod IFAUkod	

tab IFAUkod, sort

	///NOW DROP THOSE COMING FROM COUNTRIES WHERE REFUGEES WERE VERY RARE

*Missing birth country value: 
	drop if IFAUkod==.
*Nordics: 
	drop if IFAUkod ==0 | IFAUkod==26 | IFAUkod==27 | IFAUkod==28 
*Europeans: 	
	drop if IFAUkod>30 & IFAUkod<39
*Americans: 
	drop if IFAUkod==39
*Oceania: 
	drop if IFAUkod==52
*Unknown/stateless: 
capture	drop if IFAUkod==53
	
///CAN MERGE ON DEATH DATE (NOT USED CURRENTLY)

*1:m cause some die twise apparently
*merge 1:m LopNr using "RTB\DodDat", keepusing(DodDatum SenPnr)
*	drop if _merge==2
*	drop _merge SenPnr
	*duplicates report LopNr - OK
	
	///CAN MERGE ON SAMS-DATA (NOT USED)
*merge 1:m LopNr using "SAMS\SAMS_19`y'"
*Keep only those with information
*keep if _merge==3
	*duplicates report - OK
*	drop _merge



///MERGE ON MORE BIRTH DATA TO GET SEX AND AGE
*1:m again
merge 1:m LopNr using "RTB\Fodelseuppg", keepusing(Kon UtlSvBakG FodArMan)
	keep if _merge==3
 	duplicates report LopNr
	drop _merge 

*Rename sex-variable and make Men= 1, women= 0.
rename Kon sex
replace sex=0 if sex==2

	
///CREATE YEAR AND AGE VARIABLE
gen year=19`y'

tostring FodArMan, replace
gen year_born = substr(FodArMan, 1, 4) 
destring year_born, replace

gen age =year-year_born

save  "E:\ProjData\Ethnic Enclaves, political participation\temp\temp`y'", replace

}


///MERGE ON LISA DATA FOR SOCIOECONOMIC STUFF - note that this exists from 1990
*hence for the years up to 1990, use the file from 1990

foreach y in 87 88 89 90 {

use  "E:\ProjData\Ethnic Enclaves, political participation\temp\temp`y'"

*Duplicates again
merge 1:m LopNr using "LISA\LISA_1990"
*Keep only those with information - other are not immigrants in year `y'
drop if _merge==2
*Duplicates a few people - drop (for example 12 obs 1987)
	duplicates tag LopNr, gen(tag)
	drop if tag==1
	drop tag _merge
	
save  "E:\ProjData\Ethnic Enclaves, political participation\temp\temp`y'", replace

}

///1991

use  "E:\ProjData\Ethnic Enclaves, political participation\temp\temp91"

*Duplicates again
merge 1:m LopNr using "LISA\LISA_1991"
*Keep only those with information
drop if _merge==2
*Duplicates 1 person - drop
	duplicates tag LopNr, gen(tag)
	drop if tag==1
	drop tag _merge
	
save  "E:\ProjData\Ethnic Enclaves, political participation\temp\temp91", replace


*********************************************************************************
* NOW MERGE ON FAMILY DATA TO DROP THOSE WITH SIBLINGS OR FAMILY IN THE COUNTRY *
*********************************************************************************

foreach y in 87 88 89 90 91 {
///Siblingdata

use  "E:\ProjData\Ethnic Enclaves, political participation\temp\temp`y'"
gen sample=1
	
	merge 1:1 LopNr using  "E:\ProjData\Ethnic Enclaves, political participation\temp\hushåll`y'"
	keep if sample==1
	drop _merge
	
		merge 1:1 LopNr using  "E:\ProjData\Ethnic Enclaves, political participation\temp\syskon`y'"
	keep if sample==1
	drop _merge


*drop if family
	drop if anhorig==1
	drop if sibling_im==1
	drop sample

save  "E:\ProjData\Ethnic Enclaves, political participation\temp\temp`y'", replace
	
}


***************************
*ADD ENCLAVE DATA *
**************************
foreach y in 87 88 89 90 91 {
use  "E:\ProjData\Ethnic Enclaves, political participation\temp\temp`y'"
merge m:1 Forsamling using  "E:\ProjData\Ethnic Enclaves, political participation\temp\enclave_parish`y'"

drop if LopNr==.
drop _merge

gen coethnics = country2 if IFAUkod==2+24
foreach m of numlist 2(1)29 {
replace coethnics = country`m' if IFAUkod==`m'+24
} 

gen voted = voted2 if IFAUkod==2+24
foreach e of numlist 2(1)29 {
replace voted = voted`e' if IFAUkod==`e'+24
} 

gen right = right2 if IFAUkod==2+24
foreach g of numlist 2(1)29 {
replace right = right`g' if IFAUkod==`g'+24
} 

gen treiman = treiman2 if IFAUkod==2+24
foreach c of numlist 2(1)29 {
replace treiman = treiman`c' if IFAUkod==`c'+24
} 

gen educ = educ2 if IFAUkod==2+24
foreach z of numlist 2(1)29 {
replace educ = educ`z' if IFAUkod==`z'+24
} 

gen inc = inc2 if IFAUkod==2+24
foreach x of numlist 2(1)29 {
replace inc = inc`x' if IFAUkod==`x'+24
} 

*Drop the original country variable
foreach d of numlist 1(1)29 {
drop country`d'
drop voted`d'
drop right`d'
drop treiman`d'
drop inc`d'
drop educ`d'
drop im`d'
} 


duplicates drop LopNr, force /*1 dublett 89*/
save  "E:\ProjData\Ethnic Enclaves, political participation\temp\temp`y'", replace
}

///Merge with moving data

foreach y in 88 89 90 91 {
use "E:\ProjData\Ethnic Enclaves, political participation\temp\temp`y'"
gen sample = 1
merge 1:1 LopNr using "E:\ProjData\Ethnic Enclaves, political participation\temp\movers_`y'"
keep if sample==1
drop sample _merge


save "E:\ProjData\Ethnic Enclaves, political participation\temp\temp`y'", replace
}

///Now Create Panel

use  "E:\ProjData\Ethnic Enclaves, political participation\temp\temp87"
sort LopNr year
foreach y in 88 89 90 91 {
append using  "E:\ProjData\Ethnic Enclaves, political participation\temp\temp`y'"
sort LopNr year
xtset LopNr year
}

*drop a few duplicates who are in more than one year
duplicates tag LopNr, gen(tag)
drop if tag==1
drop tag


///merge on the election data from 1994

merge 1:m LopNr using "VD\Valdelt_1994rkl"
*Keep only those left in the data in the election 1994, others we cannot analyse
keep if _merge==3
drop _merge

///merge on data on radical right party voting on municipal level from 1991
merge m:1 Kommun using "E:\ProjData\Ethnic Enclaves, political participation\temp\nd91",
drop _merge

///drop ind who do not have the right to vote in municiapal elections
///will throw out those who were young when they immigrated to be able to vote

drop if k==3

///now create random order that can be used do divide into quartiles - used once!
set seed 1
generate order = uniform()

save  "E:\ProjData\Ethnic Enclaves, political participation\use\ind_panel", replace

///if one wants to use the order we use: save only the order and the LopNr

keep LopNr order
save  "E:\ProjData\Ethnic Enclaves, political participation\use\order", replace

*log close
